<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Category_model  extends MY_Model  {

    public $table = 'category';
    public $menu = '';

	public function __construct() {
		$this->class_name = get_class ( $this );
		parent::__construct( $this->class_name, $this->table );
		parent::init( $this->class_name, $this->table );
	}
	
	/**
	 * 
	 * Returns a category by its id
	 * @param int $id
	 */
	public function get($id){
		$result =  $this->db->select()->from($this->table)->where('id',$id)->get()->row();
    	$obj = FALSE;
    	if ($result){
    		$obj = $this->from_array((array)$result);
    	}
    	
    	return $obj;
	}
	
	/**
	 * 
	 * Checks if the category already exists
	 * @param string $category
	 * @param integer $parent
	 * @param integer $language
	 */
	public function existsCategory($category, $parent, $language) {

        $result =  $this->db->select()->from($this->table)->where('name = "' . $category . '" AND parent_id = ' . (int) $parent . ' AND language_id = ' . $language . '')->get()->row();
        $obj = FALSE;
        if ($result) {
            $obj = $this->from_array((array)$result);
        }
        return $obj;	
	}
	
	/**
	 * 
	 * Get records by parent
	 * @param integer $parent
	 */
	public function getByParent($parent = 0) {
        return $this->db->select()->from($this->table)->where('parent_id = ' . (int) $parent . ' AND language_id = ' . (int) $this->config->item('lang_obj')->id . ' AND display = 1')->order_by('order')->get()->result();
	
	}
	
	/**
	 * 
	 * Gets the menu
	 */
	public function get_menu() {
		$this->genarate_menu();

		return $this->menu;
	}
	
	/**
	 * 
	 * Generates the menu
	 * @param integer $id
	 * @param integer $level
	 */
	private function genarate_menu($id = 0, $level = 0) {
		$categories = $this->getByParent($id);
		//echo $this->db->last_query();
		
		$extra_class = '';
		if ($level == 1){
			$currentId = $categories[0]->id;
			if ($this->config->item('lang_obj')->id != $this->config->item('def_language_id')) {
				$currentId = $categories[0]->original_id;	
			}
			if (count($this->getByParent($currentId))) {
				$extra_class = 'hover';
			}
		}
		
		$this->menu .= '<ul class="category-' . $level . ' ' . $extra_class . '">';
		foreach ($categories as $category) {
			if ($category->parent_id == 0) {
				$level = 0;
			}
			$currentId = $category->id;
			if ($this->config->item('lang_obj')->id != $this->config->item('def_language_id')) {
				$currentId = $category->original_id;	
			}	
			$user = $this->session->userdata('user');
			if ($user && $user['admin']){
				$edit = '<a href="' . site_url('admin/category_manager/edit/' . $category->id ) . '"><span class="ui-icon ui-icon-pencil" style="display:inline-block;"></span></a>';
			} else {
				$edit = '';
			}
			$this->menu .= '<li><div><a class="menu-item" href="' . site_url('product_list/0/' . $category->id . '-' . $this->string_to_url($category->name)) . '">' . $category->name . '</a>' . $edit;
			if (count($this->getByParent($currentId))) {
				$this->menu .= $this->genarate_menu($currentId, $level+1) . '</div></li>';
			}
			else {
				$this->menu .= '</li>';

			}
		}
		$this->menu .= '</ul>';
	
		
	}
	
	function string_to_url($string){
		$string = preg_replace('/[^a-zA-Z0-9\s]/', '', $string);
		//$search = array('Ã', 'ã', 'Â', 'â', 'Î', 'î', 'ª', 'º', 'Þ', 'þ', 'ª', 'º', 'Þ', 'þ');
		$search = array("\xC4\x82", "\xC4\x83", "\xC3\x82", "\xC3\xA2", "\xC3\x8E", "\xC3\xAE", "\xC8\x98", "\xC8\x99", "\xC8\x9A", "\xC8\x9B", "\xC5\x9E", "\xC5\x9F", "\xC5\xA2", "\xC5\xA3");
		$replace = array('A', 'a', 'A', 'a', 'I', 'i', 'S', 's', 'T', 't', 'S', 's', 'T', 't');
		$out = strtolower(str_replace($search, $replace, $string));
		$search = array(" ",",","\"","'","„","”","(",")",":",".","?","!","\\","/","%");
		$replace = array('-','','','','','','','','','','','','','','');
		$out = strtolower(str_replace($search, $replace, $out));
	
		return $out;
	}
	
	/**
	 * Returns all category in function of the defined filters
	 * @param array $filter
	 */
	
	public function getAll($filter = array()){
		if (isset($filter ['field']) and strlen($filter ['field'])) {
            $this->db->select($filter ['field']);
        } else {
            $this->db->select();
        }
        
	 	if (isset($filter['parent_id']) AND strlen($filter['parent_id'])) {
            $this->db->where('parent_id', $filter['parent_id']);
        }
        
	 	if (isset($filter['language_id']) AND strlen($filter['language_id'])) {
            $this->db->where('language_id', $filter['language_id']);
        }
	 	
        if (isset($filter['original_id']) AND strlen($filter['original_id'])) {
            $this->db->where('original_id', $filter['original_id']);
        }
	 
        $this->db->order_by('order','ASC');
        $query = $this->db->get($this->table);
        
        return $query;
	}
}